package com.kupurui.asteacher.ui.job;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.android.frame.util.AppJsonUtil;
import com.facebook.drawee.view.SimpleDraweeView;
import com.kupurui.asteacher.R;
import com.kupurui.asteacher.bean.CorrectInfo;
import com.kupurui.asteacher.http.Teacher;
import com.kupurui.asteacher.ui.BaseFgt;
import com.kupurui.asteacher.ui.ShowBigImageAty;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import okhttp3.Call;
import okhttp3.Response;

/**
 * Created by Administrator on 2017/8/9.
 */

public class CorrectFgt extends BaseFgt {
    @Bind(R.id.tv_all)
    TextView tvAll;
    @Bind(R.id.simpledraweeview)
    SimpleDraweeView simpledraweeview;
    @Bind(R.id.tv_errors)
    TextView tvErrors;
    @Bind(R.id.tv_half_correct)
    TextView tvHalfCorrect;
    @Bind(R.id.tv_correct)
    TextView tvCorrect;
    @Bind(R.id.tv_num)
    TextView tvNum;
    @Bind(R.id.tv_size)
    TextView tvSize;
    @Bind(R.id.tv_name)
    TextView tvName;
    @Bind(R.id.tv_status)
    TextView tvStatus;
    @Bind(R.id.tv_serial_number)
    TextView tvSerialNumber;

    CorrectInfo correctInfo;
    private int position = 0;
    private int size = 0;

    private String correct = "";
    private CorrectAty correctAty;

    List<String> uris;

    @Override
    public int getLayoutId() {
        return R.layout.correct_fgt;
    }

    @Override
    public void initData() {
        uris = new ArrayList<>();

        correctAty = (CorrectAty) getActivity();

        if (getArguments()!=null){
            position = getArguments().getInt("position");
            size = getArguments().getInt("size");
            correctInfo = (CorrectInfo) getArguments().getSerializable("bean");
        }
        tvSerialNumber.setText("("+correctInfo.getSerial_number()+")");
        tvNum.setText((position+1)+"");
        tvSize.setText("/"+size+"");
        tvName.setText(correctInfo.getUsername());
        simpledraweeview.setImageURI(correctInfo.getContent());
        uris.add(correctInfo.getContent());

        if (!correctInfo.getStatus().equals("1")){
            tvStatus.setVisibility(View.VISIBLE);
        }


    }

    @Override
    public void requestData() {

    }
    @OnClick({R.id.tv_all,R.id.tv_errors,R.id.tv_half_correct,R.id.tv_correct,R.id.simpledraweeview})
    @Override
    public void onClick(View view) {
        super.onClick(view);
        switch (view.getId()){
            case R.id.tv_all://全部
                Bundle bundle = new Bundle();
                bundle.putString("id",correctInfo.getTest_id());
                bundle.putString("q_id",correctInfo.getQ_id());
                startActivity(CorrectDetailsAty.class,bundle);
                break;
            case R.id.tv_errors://错误
                if (correctInfo.getStatus().equals("0")){
                    showToast("上次选择和这次选择一样");
                    return;
                }else {
                    correct = "4";
                    correct();
                }
                break;
            case R.id.tv_half_correct://半对
                if (correctInfo.getStatus().equals("3")){
                    showToast("上次选择和这次选择一样");
                    return;
                }else {
                    correct = "3";
                    correct();
                }
                break;
            case R.id.tv_correct://正确
                if (correctInfo.getStatus().equals("2")){
                    showToast("上次选择和这次选择一样");
                    return;
                }else {
                    correct = "2";
                    correct();
                }
                break;
            case R.id.simpledraweeview:
                Bundle bundle1 = new Bundle();
                bundle1.putStringArrayList("pics",new ArrayList<>(uris));
                startActivity(ShowBigImageAty.class,bundle1);
                break;
        }
    }

    @Override
    public void onSuccess(String result, Call call, Response response, int what) {
        switch (what){
            case 0:
                showToast(AppJsonUtil.getResultInfo(result).getMessage());
                tvStatus.setVisibility(View.VISIBLE);
                if (correct.equals("2")){
                    correctInfo.setStatus("2");
                }else if(correct.equals("3")){
                    correctInfo.setStatus("3");
                }else{
                    correctInfo.setStatus("0");
                }
                break;
        }
        super.onSuccess(result, call, response, what);
    }

    private void correct(){
        showLoadingDialog("");
        new Teacher().correct(correctInfo.getId(),correct,this,0);
    }
}
